<?php
/**
 * Created by PhpStorm.
 * User: lwt
 * Date: 4/22/19
 * Time: 8:38 PM
 */

namespace app\admin\controller;

use think\Request;
use app\admin\model\CommodityModel;


class Commodity
{
    /**
     * 添加商品信息
     * @return \think\response\Json
     */
    public function addCommodity(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $data = $request->post();
        $config = [
            'size' => 2097152,
            'ext'  => 'jpg,gif,png,bmp,txt,zip'
        ];
        $request = Request::instance();
        $file = $request->file("co_img");
        $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');
        $save_path   = '/public/uploads/';
        $info        = $file->validate($config)->move($upload_path);

        if ($info) {

            $msg = [
                'co_name' => $data['co_name'],
                'co_volume' => $data['co_volume'],
                'co_vol' => $data['co_vol'],
                'co_content' => $data['co_content'],
                'co_time' => time(),
                'co_capacity' => $data['co_capacity'],
                'co_img' => str_replace('\\', '/', $save_path . $info->getSaveName())
            ];
            $upload = CommodityModel::addCommodity($msg);
            if ($upload == 0) {
                $content["code"] = 400;
                $content["msg"] = "商品已存在！";
                return json($content);
            } else {
                $content["code"] = 200;
                $content["msg"] = "商品添加成功！";
                return json($content);
            }
        } else {
            $result = [
                'code'   => 400,
                'msg' => $file->getError()
            ];
            return json($result);
        }

    }

    /**
     * 批量刪除商品信息
     * @return \think\response\Json
     */
    public function deleteCommodity(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $data = $request->post();
        $coIdList = explode(",",$data["co_id"]);
        $num = count($coIdList);
        $deleteNum = 0;

        $msg = array();
        for ($x=0; $x<$num; $x++){
            $a = CommodityModel::deleteCommodity($coIdList[$x]);
            if ($a == 0){
                $msg["code"] = 400;
                $msg["deleteNum"] = "已刪除".$deleteNum."條，id：".$coIdList[$x]."不存在";
                $msg["msg"] = "刪除異常！";
                return json($msg);
            }

            $deleteNum = $deleteNum + $a;
        }
        $msg["code"] = 200;
        $msg["deleteNum"] = "已刪除".$deleteNum."條";
        $msg["msg"] = "刪除成功！";

        return json($msg);
    }

    /**
     * 更新商品信息
     * @return \think\response\Json
     */
    public function updateCommodity(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $data = $request->post();
        $config = [
            'size' => 2097152,
            'ext'  => 'jpg,gif,png,bmp,txt,zip'
        ];
        $request = Request::instance();
        $file = $request->file("co_img");
        $isImg = true;
        if (empty($file)){
            $info        = true;
            $isImg = false;
        }else{
            $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');
            $save_path   = '/public/uploads/';
            $info        = $file->validate($config)->move($upload_path);
            $isImg = true;
        }

        if ($info) {

            $msg = [
                'co_id' => $data['co_id'],
                'co_name' => $data['co_name'],
                'co_volume' => $data['co_volume'],
                'co_vol' => $data['co_vol'],
                'co_content' => $data['co_content'],
                'co_time' => time(),
                'co_capacity' => $data['co_capacity']
            ];
            if ($isImg){
                $msg["co_img"] = str_replace('\\', '/', $save_path . $info->getSaveName());
            }
            $upload = CommodityModel::updateCommodity($msg);
            if ($upload == 0) {
                $content["code"] = 400;
                $content["msg"] = "更改失败！请检查商品ID是否存在！！";
                return json($content);
            } else {
                $content["code"] = 200;
                $content["msg"] = "更改成功！";
                return json($content);
            }
        } else {
            $result = [
                'code'   => 400,
                'msg' => $file->getError()
            ];
            return json($result);
        }

    }


    /**
     * 查詢所有商品信息
     * @return \think\response\Json
     */
    public function selectAllCommodity(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $content = array();
        $data = CommodityModel::selectAllCommodity();
        $content["code"] = 200;
        $content["num"] = count($data);
        $content["msg"] = $data;
        return json($content);
    }

    /**
     * 模糊查詢商品信息
     * @return \think\response\Json
     */
    public function selectLikeCommodity(){
        $request = Request::instance();
        $data = $request->param();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $content = array();
        $data = CommodityModel::selectLikeCommodity($data["co_name"]);
        $content["code"] = 200;
        $content["num"] = count($data);
        $content["msg"] = $data;
        return json($content);
    }

    /**
     * 根據商品id查詢商品信息
     * @return \think\response\Json
     */
    public function selectCommodityById(){
        $request = Request::instance();
        $data = $request->param();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $content = array();
        $data = CommodityModel::selectCommodityById($data["co_id"]);
        $content["code"] = 200;
        $content["num"] = count($data);
        $content["msg"] = $data;
        return json($content);
    }
}